package com.paishen.peiniwan;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.paishen.common.util.BaseUtil;
import com.paishen.common.util.SpfsUtil;

/**
 * Created by wp on 2016/4/13.
 */
public class Once {

    private Once() {
    }

    private final static Once once = new Once();

    boolean hsMainMakeDate, hsProfileInfo, hsDateChoose, hsMainDateMenu;
    boolean hsDateStartMale, hsDateStartFemale;

    public static Once getInstance() {
        return once;
    }


    public void coverMainMakeDate(Context context, ViewGroup parent) {
        if (hsMainMakeDate) return;

        boolean hasShown = SpfsUtil.readBoolean("cover_main_make_date", false);

        if (!hasShown) {
            hsMainMakeDate = true;
            
            final View cover = LayoutInflater.from(context).inflate(R.layout.cover_main_make_date, null);
            cover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SpfsUtil.write("cover_main_make_date", true);
                    cover.setVisibility(View.GONE);
                }
            });

            parent.addView(cover);
        }
    }


    public void coverProfileInfo(Context context, ViewGroup parent) {
        if (hsProfileInfo) return;

        boolean hasShown = SpfsUtil.readBoolean("cover_profile_info", false);
        
        if (!hasShown) {
            hsProfileInfo = true;
            
            final View cover = LayoutInflater.from(context).inflate(R.layout.cover_profile_info, null);
            cover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SpfsUtil.write("cover_profile_info", true);
                    cover.setVisibility(View.GONE);
                }
            });
            parent.addView(cover);
        }
    }

    public void coverDateChoose(Context context, ViewGroup parent) {
        if (hsDateChoose) return;

        boolean hasShown = SpfsUtil.readBoolean("cover_date_choose", false);
        
        if (!hasShown) {
            hsDateChoose = true;
            
            int resId = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? R.mipmap.cover_date_choose : R.mipmap.cover_date_choose_4_4;
            final View cover = LayoutInflater.from(context).inflate(R.layout.cover_date_choose, null);
            ((ImageView) cover.findViewById(R.id.img_cover)).setImageResource(resId);
            cover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SpfsUtil.write("cover_date_choose", true);
                    cover.setVisibility(View.GONE);
                }
            });
            parent.addView(cover);
        }
    }

    public void coverMainDateMenu(Context context, ViewGroup parent) {
        if (hsMainDateMenu) return;

        boolean hasShown = SpfsUtil.readBoolean("cover_main_date_menu", false);

        if (!hasShown) {
            hsMainDateMenu = true;
            
            int resId = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? R.mipmap.cover_main_date_menu : R.mipmap.cover_main_date_menu_4_4;
            final View cover = LayoutInflater.from(context).inflate(R.layout.cover_main_date_menu, null);
            ((ImageView) cover.findViewById(R.id.img_cover)).setImageResource(resId);
            cover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SpfsUtil.write("cover_main_date_menu", true);
                    cover.setVisibility(View.GONE);
                }
            });
            parent.addView(cover);
        }
    }

    public void coverDateStart(Context context, boolean male, ViewGroup parent) {
        if (male) {
            if (hsDateStartMale) return;

            boolean hasShown = SpfsUtil.readBoolean("cover_date_start_male", false);

            if (!hasShown) {
                hsDateStartMale = true;

                final View cover = LayoutInflater.from(context).inflate(R.layout.cover_date_start, null);
                View top = cover.findViewById(R.id.view_top);
                int height = (int) context.getResources().getDimension(com.paishen.common.R.dimen.title_height) + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? BaseUtil.getStatusBarHeight() : 0);
                top.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height));

                ((ImageView) cover.findViewById(R.id.img_cover_2)).setImageResource(R.mipmap.cover_date_start_2_male);
                cover.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SpfsUtil.write("cover_date_start_male", true);
                        cover.setVisibility(View.GONE);
                    }
                });
                parent.addView(cover);
            }
        } else {
            if (hsDateStartFemale) return;

            boolean hasShown = SpfsUtil.readBoolean("cover_date_start_female", false);

            if (!hasShown) {
                hsDateStartFemale = true;


                final View cover = LayoutInflater.from(context).inflate(R.layout.cover_date_start, null);
                View top = cover.findViewById(R.id.view_top);
                int height = (int) context.getResources().getDimension(com.paishen.common.R.dimen.title_height) + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? BaseUtil.getStatusBarHeight() : 0);
                top.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height));

                ((ImageView) cover.findViewById(R.id.img_cover_2)).setImageResource(R.mipmap.cover_date_start_2_female);
                cover.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SpfsUtil.write("cover_date_start_female", true);
                        cover.setVisibility(View.GONE);
                    }
                });
                parent.addView(cover);
            }
        }
    }


    public void clearHistory() {
        SpfsUtil.write("cover_main_make_date", hsMainMakeDate = false);
        SpfsUtil.write("cover_profile_info", hsProfileInfo = false);
        SpfsUtil.write("cover_date_choose", hsDateChoose = false);
        SpfsUtil.write("cover_main_date_menu", hsMainDateMenu = false);
        SpfsUtil.write("cover_date_start_male", hsDateStartMale = false);
        SpfsUtil.write("cover_date_start_female", hsDateStartFemale = false);
    }
}
